package com.daka.controller;

import com.daka.entity.AdministrativeRegions;
import com.daka.entity.BaseAddress;
import com.daka.entity.UserPassportApplyOrder;
import com.daka.enumerate.StatusCode;
import com.daka.service.AdministrativeRegionsService;
import com.daka.service.BaseAddressService;
import com.daka.service.UserPassportApplyOrderService;
import com.daka.tools.DateUtil;
import com.daka.vo.Page;
import com.daka.vo.request.BaseAddressRequestVo;
import com.daka.vo.request.PassportRequestVo;
import com.daka.vo.response.BassAddressResponseSimpl;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.passport.PassportResponseCMS;
import com.daka.vo.response.passport.PassportResponseDetCMS;
import com.daka.vo.response.passport.PassportResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/10/24 14:42
 */

@Controller
@RequestMapping("/passport")
@Slf4j
public class PassportController {

    @Resource
    private BaseAddressService baseAddressService;
    @Resource
    private UserPassportApplyOrderService userPassportApplyOrderService;
    @Resource
    private AdministrativeRegionsService administrativeRegionsService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryAddressList", consumes = "application/json")
    public DataResponseVo<Object> queryAddressList(@RequestBody BaseAddressRequestVo po) {
        if(po.getArea()!=null && po.getArea()==""){
            return new DataResponseVo<Object>(StatusCode.PARAM_ERROR.getStatusCode(),StatusCode.PARAM_ERROR.getStatusMessage());
        }
        List<BaseAddress> list = baseAddressService.selectAddressByConditions(po);
        int count = baseAddressService.selectAddressByConditionsCount(po);
        List<BassAddressResponseSimpl> list1 = new ArrayList<>();
        if(list.size()>0){
            for(BaseAddress aa:list){
                list1.add(BassAddressResponseSimpl.buildVO(aa));
            }
        }
        Page<BassAddressResponseSimpl> page = new Page<>();
        page.setData(list1);
        page.setTotalCount(count);
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(),page);
    }

    //列表
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryList", consumes = "application/json")
    public DataResponseVo<Object> queryList(@RequestBody PassportRequestVo requestVo, HttpSession session) {
        Page<PassportResponseVo> page =userPassportApplyOrderService.selectPageByStatement(requestVo);

        if(page!=null){
            if(page.getData()!=null){
                for(PassportResponseVo aa :page.getData()){
                    if(aa.getApplyType()==1){
                        aa.setApplyTypeStr("首次申请");
                    }else if(aa.getApplyType()==2){
                        aa.setApplyTypeStr("遗失补办");
                    }else if(aa.getApplyType()==3){
                        aa.setApplyTypeStr("退回重制");
                    }else{
                        aa.setApplyTypeStr("暂无类型");
                    }

                    if(aa.getApplyForm()==1){
                        aa.setApplyFormStr("在线");
                    }else if(aa.getApplyForm()==2){
                        aa.setApplyFormStr("现场");
                    }else{
                        aa.setApplyFormStr("暂无类型");
                    }
                }
            }
        }
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(),page);
    }

    /**
     * num、signDate、baseAddress
     * @param requestVo
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryByDate", consumes = "application/json")
    public DataResponseVo<Object> queryByDate(@RequestBody PassportRequestVo requestVo, HttpSession session) {
        Page<PassportResponseVo> page =userPassportApplyOrderService.selectPageByDate(requestVo);
        if(page!=null){
            if(page.getData()!=null){
                for(PassportResponseVo aa :page.getData()){
                    if(aa.getApplyType()==1){
                        aa.setApplyTypeStr("首次申请");
                    }else if(aa.getApplyType()==2){
                        aa.setApplyTypeStr("遗失补办");
                    }else if(aa.getApplyType()==3){
                        aa.setApplyTypeStr("退回重制");
                    }else{
                        aa.setApplyTypeStr("暂无类型");
                    }

                    if(aa.getApplyForm()==1){
                        aa.setApplyFormStr("在线");
                    }else if(aa.getApplyForm()==2){
                        aa.setApplyFormStr("现场");
                    }else{
                        aa.setApplyFormStr("暂无类型");
                    }
                }
            }
        }
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(),page);
    }


    /**
     * passportId、bassAddressId
     * @param requestVo
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/changeBaseAddress", consumes = "application/json")
    public DataResponseVo<Object> changeBaseAddress(@RequestBody PassportRequestVo requestVo, HttpSession session) {
        if(requestVo.getPassportId() == null || requestVo.getPassportId()==0 || requestVo.getAddressId() == null || requestVo.getAddressId() == 0){
            return new DataResponseVo<Object>(StatusCode.PARAM_ERROR.getStatusCode(),StatusCode.PARAM_ERROR.getStatusMessage());
        }
        UserPassportApplyOrder userPassportApplyOrder = userPassportApplyOrderService.selectByPrimaryKey(requestVo.getPassportId());
        BaseAddress baseAddress = baseAddressService.selectById(requestVo.getAddressId());
        userPassportApplyOrder.setBassAddressId(baseAddress.getId());
        userPassportApplyOrder.setSignAddress(baseAddress.getAddress());
        userPassportApplyOrder.setSignUnti(baseAddress.getName());
        //signDate+11天
        if(userPassportApplyOrder.getSignDate()==null){
            return new DataResponseVo<Object>(StatusCode.PASSPORT_PARAM_ERROR.getStatusCode(),StatusCode.PASSPORT_PARAM_ERROR.getStatusMessage());
        }
        String date ="";
        try {
            date = DateUtil.getAfterDate(DateUtil.getDateFormString(userPassportApplyOrder.getSignDate()+" 00:00:00"), 11).substring(0,10);
        }catch (Exception e){
            return new DataResponseVo<Object>(StatusCode.PASSPORT_PARAM_ERROR.getStatusCode(),StatusCode.PASSPORT_PARAM_ERROR.getStatusMessage());
        }
        userPassportApplyOrder.setSignDate(date);
        int a = userPassportApplyOrderService.update(userPassportApplyOrder);
        if(a == 0){
            return new DataResponseVo<Object>(StatusCode.UPDATE_ERROR.getStatusCode(),StatusCode.UPDATE_ERROR.getStatusMessage());
        }
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage());
    }

    //上海区域查询
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryArea", consumes = "application/json")
    public DataResponseVo<Object> queryArea() {
        List<AdministrativeRegions> list = administrativeRegionsService.selectSH();
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(),list);
    }

    /**
     * cms查询护照详情：name,num,passportNo,status
     * @param requestVo
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryPassportDet", consumes = "application/json")
    public DataResponseVo<Object> queryPassportDet(@RequestBody PassportRequestVo requestVo) {
        if(requestVo.getNum()==null && requestVo.getPassportNo()==null){
            return new DataResponseVo<Object>(StatusCode.PARAM_ERROR.getStatusCode(),StatusCode.PARAM_ERROR.getStatusMessage());
        }
        if(requestVo.getName()==null || requestVo.getName()==""){
            return new DataResponseVo<Object>(StatusCode.PARAM_ERROR.getStatusCode(),StatusCode.PARAM_ERROR.getStatusMessage());
        }
        //num  or old num
        List<UserPassportApplyOrder> list = userPassportApplyOrderService.selectDetByOrder(requestVo);
        if(list.size()>0){
            BaseAddress baseAddress =new BaseAddress();
            if(list.get(0).getBassAddressId()!=null && list.get(0).getBassAddressId()!=0){
                baseAddress = baseAddressService.selectById(list.get(0).getBassAddressId());
            }
            PassportResponseDetCMS passportResponseDetCMS = PassportResponseDetCMS.buildVO(list.get(0),baseAddress);
            return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(),passportResponseDetCMS);
        }else{
            return new DataResponseVo<Object>(StatusCode.SELECT_ITEM_NOT_EXISTS.getStatusCode(),StatusCode.SELECT_ITEM_NOT_EXISTS.getStatusMessage());
        }
    }

    /**
     * cms护照列表查询：
     * @param requestVo
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryPassportList", consumes = "application/json")
    public DataResponseVo<Object> queryPassportList(@RequestBody PassportRequestVo requestVo) {
        Page<PassportResponseCMS> page = userPassportApplyOrderService.selectPageByAddress(requestVo);
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(),page);
    }

}
